package integral

import (
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"higame/modules/log"
	"higame/modules/store"
	"higame/modules/utils"
	log2 "higame/routes/log"
	"net/http"
)

func lucky(ctx *gin.Context) {
	claims, err := store.GetClaims(ctx)
	if err != nil {
		log.Error.Println(err)
		ctx.JSON(500, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	num, err := lottery()
	if err != nil {
		log.Error.Println(err)
		ctx.JSON(500, gin.H{
			"code": 500,
			"msg":  "服务器内部出错",
			"data": nil,
		})
		return
	}
	text, err := award(claims.Audience, num)
	if err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  text,
		"data": num,
	})
}

func handleTask(ctx *gin.Context) {
	if data, err := taskHandler(ctx); err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": nil,
		})
	} else {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "任务完成",
			"data": data,
		})
	}

}

func setTask(ctx *gin.Context) {
	v, err := validatorForUpdateTask(ctx)
	if err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	if err := UpdateOneByTaskID(v.TaskID, bson.M{
		"integral": v.Integral,
		"exp":      v.EXP,
	}); err != nil {
		ctx.JSON(202, gin.H{
			"code": 202,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改成功",
		"data": nil,
	})
}

func getTasks(ctx *gin.Context) {
	claims, err := store.GetClaims(ctx)
	if err != nil {
		log.Error.Println(err)
		ctx.JSON(500, gin.H{
			"code": 500,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	tasks, _ := FindMany(1, bson.M{})
	for _, v := range tasks {
		taskName := v["task_name"].(string)
		isLoop := v["is_loop"].(bool)
		logs := log2.FindManyIntegralLog(0, bson.M{
			"username": claims.Audience,
			"remark":   taskName,
		})

		// 单次任务与没有日志
		if len(logs) > 0 {
			if isLoop == false {
				v["status_value"] = true
				v["status_text"] = "任务完成"
			}
		} else {
			v["status_value"] = false
			v["status_text"] = "未完成"
		}
	}
	count, _ := Count(bson.M{})
	str := ""
	if count == 0 {
		str = "没有查询到数据！"
	} else {
		str = utils.JoinString("共%v页%v行 当前在第%v页", count/20+1, count, 1)
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  str,
		"data": bson.M{
			"list":  tasks,
			"total": count,
		},
	})
}

func getPrizes(ctx *gin.Context) {
	prizes, _ := FindPrize()

	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": bson.M{
			"list":  prizes,
			"total": len(*prizes),
		},
	})
}

func setPrice(ctx *gin.Context) {
	v, err := validatorForSetPrize(ctx)
	if err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	if err := UpdateOneByPrizeName(v.Name, bson.M{
		"odds":  v.Odds,
		"prize": v.Prize,
	}); err != nil {
		ctx.JSON(202, gin.H{
			"code": 202,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "修改成功",
		"data": nil,
	})
}

func winners(ctx *gin.Context) {
	list := log2.FindManyIntegralLog(1, bson.M{
		"text": "积分抽奖",
		"integral": bson.M{
			"$gt": 0,
		},
	})
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": list,
	})
}
